package com.phonebrand.thread;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import com.phonebrand.dao.DB4CommandDao;
import com.phonebrand.global.GlobalApplication;
import com.phonebrand.model.PhoneM;
import com.phonebrand.service.PhoneService;



public class RefreshData extends Thread  {
	private static final String LOG_TAG = "RefreshData";
	private Context localContext;
	private DB4CommandDao dao = DB4CommandDao.getInstance();
	public static boolean processLoading;
	private StringBuffer strBuffer = new StringBuffer();
	
   public RefreshData(Context prm) {
	   localContext = prm;
   }
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			int i= 0;
			processLoading = true;
			Log.i(LOG_TAG, " ----------------------------------------------------------------------------------- ");
			Cursor phones = localContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        	while (phones.moveToNext())
        	{      
            	Log.i(LOG_TAG, " ----------------------------------------------------------------------------------- ");
    			            	for (String columnName : phones.getColumnNames()) {
    	    		Log.i(LOG_TAG, " columnName : "+columnName + " " +phones.getString(phones.getColumnIndex(columnName)));
    	    	}        		        		
        		try {
	        		String phoneNum =phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
	        		strBuffer.append(phoneNum+",");

	        		String displayName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
	        		PhoneM phoneM  = dao.loadPhoneMByNum(phoneNum,((GlobalApplication)localContext).getDbPhone());
	        		if (phoneM == null) {
	        			phoneM = new PhoneM();
	        			phoneM.setName(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
	              	  	phoneM.setPhoneNum(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
	        			if (phoneNum.indexOf("*") == -1) {
		              	  	String brand ="";
		              	  	if (i < 1 ) { 
		              	  		brand = PhoneService.getInstance().getBrand(phoneNum.replace("+66", "0"));	        				              	  		
		              	  	}
		              	  	phoneM.setBrandName(brand);
		        			//if (!"".equals(brand) && brand != null) {
		        			dao.storePhone(phoneM,((GlobalApplication)localContext).getDbPhone());
		        			((GlobalApplication)localContext).getDbPhone().commit();
		        			//}	
	        			}	
	        		} else {
	        			phoneM.setName(displayName);
	        			dao.storePhone(phoneM,((GlobalApplication)localContext).getDbPhone());
	        			((GlobalApplication)localContext).getDbPhone().commit();
	        		}
	        		i++;
        		} catch (Exception e) {
        			e.printStackTrace();
        		}	
        		
            	Log.i(LOG_TAG, " ----------------------------------------------------------------------------------- ");
        	}
        	
        	phones.close();
        	
        	
        	List<PhoneM> checkPhones =  dao.loadAllPhoneM(((GlobalApplication)localContext).getDbPhone());
        	
        	for (PhoneM phoneM : checkPhones) {
        		if (strBuffer.toString().indexOf(phoneM.getPhoneNum()) ==-1) {
        			dao.deletePhone(phoneM,((GlobalApplication)localContext).getDbPhone());
        			((GlobalApplication)localContext).getDbPhone().commit();
        		}
        	}

		} catch (Exception e) {
			e.printStackTrace();
		}
		processLoading = false;
		
	}	
	
	
	
}
